package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.ItemAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.ItemCategoryAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.ItemCategoryUpdateDTO;
import cn.tedu.fruitshop.pojo.entity.ItemCategory;
import cn.tedu.fruitshop.pojo.entity.ItemCategoryTree;
import cn.tedu.fruitshop.pojo.vo.ItemCategoryListItemVO;
import cn.tedu.fruitshop.pojo.vo.ItemCategoryStandardVO;
import cn.tedu.fruitshop.pojo.vo.ItemCategoryTreeVO;
import cn.tedu.fruitshop.pojo.vo.ItemListItemVO;
import cn.tedu.fruitshop.service.impl.ItemCategoryServiceImpl;
import cn.tedu.fruitshop.service.impl.ItemServiceImpl;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/itemCategory")
@Api(tags = "03. 商品分类管理模块")
@Validated
public class ItemCategoryController {
    @Autowired
    ItemCategoryServiceImpl itemCategoryService;


    public ItemCategoryController() {
        log.debug("创建控制器对象：CarController");
    }


    @ApiOperation("添加商品分类")
    @ApiOperationSupport(order = 130)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid ItemCategoryAddNewDTO itemCategoryAddNewDTO) {
        log.debug("开始处理【添加商品】的请求，参数：{}", itemCategoryAddNewDTO);
        if (itemCategoryAddNewDTO.getTwoItem() == null){
            itemCategoryAddNewDTO.setTwoItem(0);
        }
        itemCategoryService.addNew(itemCategoryAddNewDTO);
        log.debug("添加商品分类成功！");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除商品分类")
    @ApiOperationSupport(order = 131)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "Integer")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除商品分类失败，尝试删除的商品分类的ID无效！")
                                   @PathVariable Integer id) {
        log.debug("开始处理【根据id删除商品分类】的请求，参数：{}", id);
        itemCategoryService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询商品分类列表")
    @ApiOperationSupport(order = 132)
    @GetMapping("")
    public JsonResult<List<ItemCategoryListItemVO>> list(){
        log.debug("开始查询商品分类列表请求");
        List<ItemCategoryListItemVO> list = itemCategoryService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("修改商品分类详情")
    @ApiOperationSupport(order = 133)
    @ApiImplicitParam(name = "id", value = "商品分类id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Integer id, ItemCategoryUpdateDTO itemCategoryUpdateDTO) {
        log.debug("开始处理【修改商品分类】的请求，参数ID：{}, 新数据：{}", id, itemCategoryUpdateDTO);
        itemCategoryService.updateInfoById(id, itemCategoryUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询商品分类详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "商品分类id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ItemCategoryStandardVO> getStandardById(@PathVariable Integer id) {
        log.debug("开始处理【根据id查询商品分类详情】的请求，参数：{}", id);
        ItemCategoryStandardVO itemCategoryStandardVO = itemCategoryService.getStandardVO(id);
        return JsonResult.ok(itemCategoryStandardVO);
    }

    @ApiOperation("查询商品一级分类列表")
    @ApiOperationSupport(order = 132)
    @GetMapping("/selectByParentId")
    public JsonResult<List<ItemCategoryTree>> listByParentId(){
        log.debug("开始查询商品一级分类列表请求");
        List<ItemCategoryTree> list = itemCategoryService.listByParentId();
        return JsonResult.ok(list);
    }


    @ApiOperation("根据父级id查询商品分类列表")
    @ApiOperationSupport(order = 133)
    @GetMapping("/{id:[0-9]+}/ByTwoItem")
    public JsonResult<List<ItemCategoryTree>> listByTwoItem(@PathVariable Integer id){
        log.debug("开始根据二级分类id查询商品一级分类列表请求,参数为:{}",id);
        List<ItemCategoryTree> list = itemCategoryService.listByTwoItem(id);
        return JsonResult.ok(list);
    }
    @ApiOperation("获取商品分类树对象")
    @ApiOperationSupport(order = 134)
    @GetMapping("/all")
    public JsonResult<ItemCategoryTreeVO<ItemCategoryTree>> getTreeVO(){
        ItemCategoryTreeVO<ItemCategoryTree> treeVO = itemCategoryService.itemCategoryTree();
        log.debug("分类树对象为:{}",treeVO);
        return JsonResult.ok(treeVO);
    }

}
